package com.esm.sm800.database.manager;

import android.graphics.Bitmap;

import com.esm.sm800.activity.AppContext;
import com.esm.sm800.database.dbmodel.BornDoor;
import com.esm.sm800.database.dbmodel.Draw_report;
import com.esm.sm800.database.dbmodel.Member;
import com.esm.sm800.database.dbmodel.Needle;
import com.esm.sm800.database.dbmodel.SystemAllInfo;
import com.esm.sm800.database.dbmodel.Warehousing;
import com.esm.sm800.database.greendao.BornDoorDao;
import com.esm.sm800.database.greendao.Buzhen_reportDao;
import com.esm.sm800.database.greendao.Draw_reportDao;
import com.esm.sm800.database.greendao.HiskeyrecordDao;
import com.esm.sm800.database.greendao.MemberDao;
import com.esm.sm800.database.greendao.NeedleDao;
import com.esm.sm800.database.greendao.SystemAllInfoDao;
import com.esm.sm800.database.greendao.WarehousingDao;
import com.esm.sm800.database.greendao.Warehousing_recordDao;

import java.util.ArrayList;

public class SystemCenter {

    private static SystemCenter instance = new SystemCenter();

    public static SystemCenter getInstance() {
        return instance;
    }

    public Member member = null;//当前登录的用户
    public Member memberTgr = null;//断针通过人
    public Member memberEdit = null;//编辑的用户
    public Needle needleEdit = null;
    public Warehousing warehousingEdit = null;

    public Needle needleHz = null;//还针needle

    public Draw_report drEdit = null;//断针待编辑的还针记录

    public int logintype = 0; //0-补针 1-借针 2-还针 3-设置 4-断针通过人

    public String hzJpgpath = "";//还针即时的图片保存路径
    public Bitmap hzBitmap = null;//还针检测拍照灰度图片

    public int needlelen = 0;//采集的长度

    public SystemAllInfo getSysInfo(String key) {
        if (key == null || key.length() < 1) {
            return null;
        }
        String strContent = "";
        SystemAllInfo sInfo = null;
        try {
            SystemAllInfoDao systemAllInfoDao = AppContext.getInstances().getDaoSession().getSystemAllInfoDao();
            ArrayList<SystemAllInfo> sysList = new ArrayList<SystemAllInfo>();
            sysList = (ArrayList) systemAllInfoDao.queryBuilder()
                    .where(SystemAllInfoDao.Properties.Name.eq(key))
                    .build().list();
            if (sysList.size() > 0) {
                sInfo = sysList.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return sInfo;
    }

    public Long saveSysInfo(SystemAllInfo info) {
        if (info == null) {
            return 0L;
        }

        SystemAllInfo a = getSysInfo(info.getName());
        if (a != null) {
            info.setId(a.getId());
        }

        try {
            SystemAllInfoDao systemAllInfoDao = AppContext.getInstances().getDaoSession().getSystemAllInfoDao();
            long id = systemAllInfoDao.insertOrReplace(info);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

    //清空数据库
    public boolean clearDB() {
        try {
            SystemAllInfoDao systemAllInfoDao = AppContext.getInstances().getDaoSession().getSystemAllInfoDao();
            systemAllInfoDao.deleteAll();

            BornDoorDao BornDoorDao = AppContext.getInstances().getDaoSession().getBornDoorDao();
            BornDoorDao.deleteAll();

            Buzhen_reportDao Buzhen_reportDao = AppContext.getInstances().getDaoSession().getBuzhen_reportDao();
            Buzhen_reportDao.deleteAll();

            Draw_reportDao Draw_reportDao = AppContext.getInstances().getDaoSession().getDraw_reportDao();
            Draw_reportDao.deleteAll();

            HiskeyrecordDao HiskeyrecordDao = AppContext.getInstances().getDaoSession().getHiskeyrecordDao();
            HiskeyrecordDao.deleteAll();

            MemberDao MemberDao = AppContext.getInstances().getDaoSession().getMemberDao();
            MemberDao.deleteAll();

            NeedleDao NeedleDao = AppContext.getInstances().getDaoSession().getNeedleDao();
            NeedleDao.deleteAll();

            WarehousingDao WarehousingDao = AppContext.getInstances().getDaoSession().getWarehousingDao();
            WarehousingDao.deleteAll();

            Warehousing_recordDao Warehousing_recordDao = AppContext.getInstances().getDaoSession().getWarehousing_recordDao();
            Warehousing_recordDao.deleteAll();

        } catch (Exception e) {
            e.printStackTrace();
        }

        return true;
    }

}
